package reviewAlgorithms.dynamicProgramming;

import org.omg.PortableInterceptor.INACTIVE;

public class NumDecodings {
    public static void main(String[] args) {
        String s = "33016";
        System.out.println(numDecodings(s));
    }

    public static int numDecodings(String s) {
        if (s == null || s.length() < 1){
            return 0;
        }
        if (s.length() == 1){
            if (Integer.valueOf(s) == 0){
                return 0;
            }else {
                return 1;
            }
        }
        int[] dp = new int[s.length() + 1];
        dp[0] = 1;
        dp[1] = 1;
        for (int i = 1; i < s.length(); i++) {
            if (s.charAt(i) != '0'){
                if (s.charAt(i - 1) != '0'){
                    if (Integer.valueOf(s.substring(i - 1,i + 1)) <= 26){
                        dp[i + 1] = dp[i] + dp[i - 1];
                    }else {
                        dp[i + 1] = dp[i];
                    }
                }else {
                    if (i - 1 > 0){
                        dp[i + 1] = dp[i - 1];
                    }else {
                        return 0;
                    }
                }
            }else {
                if (s.charAt(i - 1) != '0' && Integer.valueOf(s.substring(i - 1,i + 1)) <= 26){
                    dp[i + 1] = dp[i - 1];
                }else {
                    return 0;
                }
            }
        }
        return dp[s.length()];
    }
}
